//
//  XJYLandingNextView.m
//  ShopMall
//
//  Created by XJY on 17/1/19.
//  Copyright © 2017年 XinJinYing. All rights reserved.
//

#import "XJYLandingNextView.h"

@interface XJYLandingNextView ()<UITextFieldDelegate>

@property (nonatomic, strong) UILabel *toastLabel;  //提示
@property (nonatomic, strong) UILabel *backLabel;   //背景
@property (nonatomic, strong) UITextField *vaildTF;
@property (nonatomic, strong) UILabel *lineLabel;
@property (nonatomic, strong) UIButton *timeButton;
@property (nonatomic, strong) UIButton *landingButton;

@end

@implementation XJYLandingNextView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self addSubview:self.toastLabel];
        [self addSubview:self.backLabel];
        [self addSubview:self.vaildTF];
        [self addSubview:self.lineLabel];
        [self addSubview:self.timeButton];
        [self addSubview:self.landingButton];
    }
    return self;
}

- (void)layoutSubviews
{
    [super layoutSubviews];
    __weak typeof(self) weakSelf = self;
    [_toastLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf).offset(15);
        make.height.equalTo(@30);
        make.top.equalTo(weakSelf.mas_top);
    }];
    
    [_backLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(weakSelf).offset(-1);
        make.height.equalTo(@44);
        make.top.equalTo(weakSelf.toastLabel.mas_bottom);
    }];
    
    [_vaildTF mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.mas_right).offset(-100);
        make.top.equalTo(weakSelf.backLabel.mas_top).offset(1);
        make.height.equalTo(@42);
    }];
    
    [_lineLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@1);
        make.left.equalTo(weakSelf.vaildTF.mas_right);
        make.height.equalTo(@35);
        make.centerY.equalTo(weakSelf.backLabel);
    }];
    
    [_timeButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(weakSelf.backLabel.mas_top);
        make.left.equalTo(weakSelf.lineLabel.mas_right);
        make.right.equalTo(weakSelf.mas_right);
        make.height.equalTo(@42);
    }];
    
    [_landingButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.mas_left).offset(15);
        make.right.equalTo(weakSelf.mas_right).offset(-15);
        make.height.equalTo(@35);
        make.top.equalTo(weakSelf.backLabel.mas_bottom).offset(30);
    }];

}

#pragma mark - 懒加载
- (UILabel *)toastLabel
{
    if (!_toastLabel) {
        _toastLabel = [[UILabel alloc] init];
    }
    return _toastLabel;
}

- (UILabel *)backLabel
{
    if (!_backLabel) {
        _backLabel = [[UILabel alloc] init];
        _backLabel.backgroundColor = [UIColor whiteColor];
        _backLabel.layer.borderWidth = 1;
        _backLabel.layer.borderColor = RGB(192, 192, 192).CGColor;
    }
    return _backLabel;
}

- (UITextField *)vaildTF
{
    if (!_vaildTF) {
        _vaildTF = [[UITextField alloc] init];
        _vaildTF.placeholder = @"请输入验证码";
        _vaildTF.delegate = self;
        [_vaildTF addTarget:self action:@selector(codeTextChangeText:) forControlEvents:UIControlEventEditingChanged];
    }
    return _vaildTF;
}

- (UILabel *)lineLabel
{
    if (!_lineLabel) {
        _lineLabel = [[UILabel alloc] init];
        _lineLabel.backgroundColor = RGB(192, 192, 192);
    }
    return _lineLabel;
}

- (UIButton *)timeButton
{
    if (!_timeButton) {
        _timeButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_timeButton setTitle:@"发送验证码" forState:UIControlStateNormal];
        _timeButton.titleLabel.font = [UIFont systemFontOfSize:14.0f];
        [_timeButton addTarget:self action:@selector(gcdTimer) forControlEvents:UIControlEventTouchUpInside];
        [_timeButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    }
    return _timeButton;
}

- (UIButton *)landingButton
{
    if (!_landingButton) {
        _landingButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_landingButton setTitle:@"注册" forState:UIControlStateNormal];
        [_landingButton setBackgroundColor:RGB(192, 192, 192)];
        _landingButton.userInteractionEnabled = NO;
        [_landingButton setTitleColor:RGB(132, 132, 132) forState:(UIControlStateNormal)];
        [_landingButton setTitleColor:[UIColor whiteColor] forState:(UIControlStateSelected)];
        _landingButton.selected = NO;
        [_landingButton addTarget:self action:@selector(landingClick:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    return _landingButton;
}

#pragma mark - set方法
- (void)setPhoneNum:(NSString *)phoneNum
{
    _phoneNum = phoneNum;
    _toastLabel.attributedText = [self makeToastLabelAttributed];
}

#pragma mark - 倒计时
- (void)gcdTimer
{
    //定义一个计数开始值
    __block NSInteger time = 10;
    
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue);
    dispatch_source_set_timer(timer, DISPATCH_TIME_NOW, 1.0 * NSEC_PER_SEC, 0 * NSEC_PER_SEC);
    dispatch_source_set_event_handler(timer, ^{
        if (time < 1) {
            //如果<1  按钮文字改变，并且可以点击
            dispatch_source_cancel(timer);
            NSMutableAttributedString *string = [[NSMutableAttributedString alloc]initWithString:@"重新发送" attributes:@{NSForegroundColorAttributeName:RGB(56, 166, 243)}];
            dispatch_async(dispatch_get_main_queue(), ^{
                _timeButton.userInteractionEnabled = YES;
                [_timeButton setAttributedTitle:string forState:(UIControlStateNormal)];
            });
        } else {
            //时间-1，按钮不可改变
            dispatch_async(dispatch_get_main_queue(), ^{
                _timeButton.userInteractionEnabled = NO;
                [_timeButton setAttributedTitle:[self makeTimeButtonAttributed:time] forState:UIControlStateNormal];
            });
            time--;
        }
    });
    dispatch_resume(timer);
}

#pragma mark - 计时器文字
- (NSMutableAttributedString *)makeTimeButtonAttributed:(NSInteger)time
{
    NSMutableAttributedString *string3 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%li",time] attributes:@{NSForegroundColorAttributeName : RGB(56, 166, 243), NSFontAttributeName : [UIFont systemFontOfSize:14.0f]}];
    NSMutableAttributedString *string4 = [[NSMutableAttributedString alloc] initWithString:@"秒后发送" attributes:@{NSForegroundColorAttributeName : RGB(139, 139, 139), NSFontAttributeName : [UIFont systemFontOfSize:14.0f]}];
    [string3 insertAttributedString:string4 atIndex:string3.length];
    return string3;
}

- (NSMutableAttributedString *)makeToastLabelAttributed
{
    NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc] initWithString:@"发送验证码到" attributes:@{NSForegroundColorAttributeName : RGB(139, 139, 139)}];
    NSMutableAttributedString *string2 = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" +86 %@", self.phoneNum] attributes:@{NSForegroundColorAttributeName : RGB(56, 166, 243)}];
    [string1 insertAttributedString:string2 atIndex:string1.length];
    return string1;
}

#pragma mark - 代理方法
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if (range.location == 6) {
        return NO;
    }
    return YES;
}

- (void)codeTextChangeText:(UITextField *)textField
{
    if (textField.text.length == 6) {
        _landingButton.userInteractionEnabled = YES;
        _landingButton.backgroundColor = RGB(56, 166, 241);
        _landingButton.selected = YES;
        
    } else {
        _landingButton.userInteractionEnabled = NO;
        _landingButton.backgroundColor = RGB(192, 192, 192);
        _landingButton.selected = NO;
    }
}

#pragma mark - 注册点击方法
- (void)landingClick:(UIButton *)button
{
    if (_landingBlock) {
        _landingBlock(_vaildTF.text);
    }
}

@end
